﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BUS;
using DAL;
namespace QuanLyNhaSach
{
    public partial class Form_QLNhaCungCap : DevComponents.DotNetBar.Office2007Form
    {
        Form_Them_CapNhat_NCC frm;
        NhaCungCap_BUS ncc_bus;
        NhaCungCap_DTO ncc_dto;
        public Form_QLNhaCungCap()
        {
            InitializeComponent();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frm = new Form_Them_CapNhat_NCC();
            frm.KhiThemMoi += frm_KhiThemMoi;
            frm.Action = "Insert";
            frm.ShowDialog();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            frm = new Form_Them_CapNhat_NCC();
            frm.KhiCapNhat += frm_KhiCapNhat;
            frm.Action = "Update";
            frm.ID = dtg_QLNhaCungCap.Rows[dong].Cells["Str_MaNCC"].Value.ToString();
            frm.ShowDialog();
        }
        int dong;
        private void dtg_QLTacGia_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
            txtTenNCC.Text = dtg_QLNhaCungCap.Rows[dong].Cells["Str_TenNCC"].Value.ToString();
            txtDiaChi.Text = dtg_QLNhaCungCap.Rows[dong].Cells["Str_DiaChi"].Value.ToString();
            txtDienThoai.Text = dtg_QLNhaCungCap.Rows[dong].Cells["Str_DienThoai"].Value.ToString();
            txtEmail.Text = dtg_QLNhaCungCap.Rows[dong].Cells["Str_Email"].Value.ToString();
        }
        
        List<string> MaNCC;
        private void btnXoa_Click(object sender, EventArgs e)
        {
            MaNCC = new List<string>();
            for (int i = 0; i < dtg_QLNhaCungCap.Rows.Count; i++)
            {
                if (dtg_QLNhaCungCap.Rows[i].Cells["CHECKBOX"].Value != null)
                    MaNCC.Add(dtg_QLNhaCungCap.Rows[i].Cells["Str_MaNCC"].Value.ToString());
            }
            if (MaNCC.Count == 0)
            {
                MessageBoxEx.Show("Bạn chưa chọn nhà cung cấp để xóa.", "Thông báo");
                return;
            }

            if (MessageBoxEx.Show("Bạn có chắc chắn xóa dòng này không?", "Xóa nhà cung cấp", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                for (int i = 0; i < MaNCC.Count; i++)
                {
                    ncc_dto = new NhaCungCap_DTO();
                    ncc_bus = new NhaCungCap_BUS();
                    ncc_dto.Str_MaNCC = MaNCC[i].ToString();
                    ncc_bus.XoaNCC(ncc_dto);
                }
                LoadDanhSachNhaCungCap();
                MaNCC.Clear();
            }
            else
            {
                for (int i = 0; i < dtg_QLNhaCungCap.Rows.Count; i++)
                {
                    dtg_QLNhaCungCap.Rows[i].Cells["CHECKBOX"].Value = false;
                }
            }
        }
        void LoadDanhSachNhaCungCap()
        {
            ncc_bus = new NhaCungCap_BUS();
            dtg_QLNhaCungCap.DataSource = ncc_bus.LayDanhSachNCC();
        }
        void frm_KhiThemMoi()
        {
            LoadDanhSachNhaCungCap();
        }
        void frm_KhiCapNhat()
        {
            LoadDanhSachNhaCungCap();
        }

        private void Form_QLNhaCungCap_Load(object sender, EventArgs e)
        {
            dtg_QLNhaCungCap.AutoGenerateColumns = false;
            LoadDanhSachNhaCungCap();
        }

        private void dtg_QLNhaCungCap_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            btnCapNhat_Click(sender, e);
        }

        private void dtg_QLNhaCungCap_Click(object sender, EventArgs e)
        {
            if (dtg_QLNhaCungCap.Rows[dong].Cells["CHECKBOX"].Value == null)
                dtg_QLNhaCungCap.Rows[dong].Cells["CHECKBOX"].Value = true;
            else
                dtg_QLNhaCungCap.Rows[dong].Cells["CHECKBOX"].Value = null;
        }
    }
}
